<template>
    <!-- <el-form label-width="110px" size="small">
        <el-col class="form-row"> -->
    <div>
        <el-form-item label="净价价格:">
            <RiskInputNumber
                :input="form.orderPrice"
                :inputAfter="'元'"
                :colorStyle="textColor"
                :precision="2"
                :maxlength="10"
                :field="'orderPrice'"
                @getInputNumber="getInputNumber(arguments)"
            >
            </RiskInputNumber>
        </el-form-item>
        <el-form-item label="全价价格:">
            <RiskInputNumber
                :input="form.fullPrice"
                :inputAfter="'元'"
                :colorStyle="textColor"
                :precision="2"
                :maxlength="10"
                :field="'fullPrice'"
                @getInputNumber="getInputNumber(arguments)"
            >
            </RiskInputNumber>
        </el-form-item>
        <el-form-item label="指令数量:">
            <RiskInputNumber
                :input="form.orderAmount"
                :inputAfter="'份'"
                :colorStyle="textColor"
                :maxlength="12"
                :precision="2"
                :disabled="targetType == '2'"
                :readonly="!stockInfo.stockCode"
                :field="'orderAmount'"
                @getInputNumber="getInputNumber(arguments)"
            >
            </RiskInputNumber>
        </el-form-item>
        <el-form-item label="净价金额:">
            <RiskInputNumber
                :input="form.orderBalance"
                :inputAfter="'元'"
                :colorStyle="textColor"
                :precision="2"
                :field="'orderBalance'"
                :maxlength="12"
                :disabled="true"
                @getInputNumber="getInputNumber(arguments)"
            >
            </RiskInputNumber>
        </el-form-item>
        <el-form-item label="总应计利息:">
            <RiskInputNumber
                :disabled="true"
                :input="form.totalInterest"
                :inputAfter="'元'"
                :colorStyle="textColor"
                :precision="2"
                :maxlength="12"
                :field="'totalInterest'"
                @getInputNumber="getInputNumber(arguments)"
            >
            </RiskInputNumber>
        </el-form-item>

        <el-form-item label="全价金额:">
            <RiskInputNumber
                :disabled="true"
                :input="form.fullBalance"
                :inputAfter="'元'"
                :colorStyle="textColor"
                :precision="2"
                :maxlength="12"
                :field="'fullBalance'"
                @getInputNumber="getInputNumber(arguments)"
            >
            </RiskInputNumber>
        </el-form-item>
        <!-- </el-col>
    </el-form> -->
    </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
import RiskInputNumber from '@/pages/risk-measure/components/risk-input-number.vue';
import { handleLimitAmount } from '@/utils/tools';

@Component({
    name: 'SHFixedForm',
    components: {
        RiskInputNumber,
    },
})
// 债券指令额外表单
export default class SHFixedForm extends Vue {
    @Prop()
    formDetail: any;
    @Prop()
    stockInfo: any;
    @Prop()
    textColor: string;
    @Prop()
    targetType: string; // 目标类型
    @Prop()
    notMesh: boolean;
    @Prop()
    changeFlag: string;

    form = {
        orderPrice: '', // 净价价格
        orderAmount: '', // 指令数量
        orderBalance: '', // 净价金额
        totalInterest: '', // 总应计利息
        fullPrice: '',
        fullBalance: '',
    };
    totalInterest: string = '';
    fullPrice: string = '';
    fullBalance: string = '';

    @Watch('form', { immediate: true, deep: true })
    setForm(newValue, oldValue) {
        this.$emit('getForm', this.form, 'shFixedValues');
    }
    @Watch('formDetail', { immediate: true, deep: true })
    getForm() {
        for (const key in this.formDetail) {
            this.form[key] = this.formDetail[key];
        }
    }

    @Watch('changeFlag', { immediate: true, deep: true })
    onWatchChangeFlag() {
        if (this.form.orderAmount) {
            if (this.form.orderAmount === '0.00') {
                this.yh_message_warn('指令数量必须大于0！');
                return;
            }
            let limit = handleLimitAmount(this.changeFlag, this.stockInfo, this.form.orderAmount);
            if (!limit) {
                this.form.orderAmount = '';
            }
        }
    }

    getInputNumber(args: any) {
        let value = args[0];
        let field = args[1];

        this.form[field] = value;
        if (field === 'orderAmount') {
            setTimeout(() => {
                this.handleOrderAmount(value);
            }, 10);
        } else if (field === 'orderPrice') {
            this.handleOrderPrice();
        } else if (field === 'fullPrice') {
            // this.fullPrice = value;
            this.handleFullPrice();
        }
    }

    /**
     *  处理订单数量变动
     */
    handleOrderAmount(value: any) {
        if (value) {
            // 限定指令数量
            let limit = handleLimitAmount(this.changeFlag, this.stockInfo, value);
            if (!limit) {
                this.form.orderAmount = '';
            }
            // handleLimitAmount(this.textColor, this.stockInfo, value);
        }
        if (this.form.orderPrice) {
            this.calcForm('orderPrice');
        } else if (this.form.fullPrice) {
            this.calcForm('fullPrice');
        }

        // 如果数量为0，净价金额、净价金额、应计利息 为空
        if (!this.form.orderAmount) {
            // this.fullBalance = '';
            // this.form.orderBalance = '';
            // this.totalInterest = '';
            this.form.fullBalance = '';
            this.form.orderBalance = '';
            this.form.totalInterest = '';
        }
        this.form.totalInterest = this.calcInterest(this.form.orderAmount, this.stockInfo.tDayInterest).toString();
    }

    /**
     *  处理净价价格变动 ，
     */
    handleOrderPrice() {
        if (this.form.orderPrice) {
            // 净价价格不为空，用数量和净价价格计算
            if (this.form.orderAmount) {
                this.calcForm('orderPrice');
            } else {
                // 指令数量为空时，只计算全价价格
                this.form.fullPrice = this.calcFullPrice(this.form.orderPrice, this.stockInfo.tDayInterest);
            }
        } else {
            // this.fullBalance = '';
            // this.form.orderBalance = '';
            // this.fullPrice = '';
            this.form.fullBalance = '';
            this.form.orderBalance = '';
            this.form.totalInterest = '';
        }
    }

    /**
     *  处理全价价格变动
     */
    handleFullPrice() {
        if (this.form.fullPrice) {
            if (this.form.orderAmount) {
                this.calcForm('fullPrice');
            } else {
                // 指令数量为空时，只计算净价价格
                this.form.orderPrice = this.calOrderPrice(this.form.fullPrice, this.stockInfo).toFixed(2);
            }
        } else {
            // this.fullBalance = '';
            // this.form.orderPrice = '';
            // this.form.orderBalance = '';
            this.form.fullBalance = '';
            this.form.orderPrice = '';
            this.form.orderBalance = '';
        }
    }

    calcForm(calcType) {
        console.log(calcType, 'calcType');

        // 拿净价价格计算
        if (calcType === 'orderPrice' && this.form.orderPrice) {
            // 净价金额
            this.form.orderBalance = (Number(this.form.orderPrice) * Number(this.form.orderAmount)).toFixed(2).toString();
            // 应计利息
            let interest = this.calcInterest(this.form.orderAmount, this.stockInfo.tDayInterest);
            // 全价价格
            this.form.fullPrice = this.calcFullPrice(this.form.orderPrice, this.stockInfo.tDayInterest);
            // 总应计利息
            this.form.totalInterest = interest.toString();
            // 全价金额
            this.form.fullBalance = (Number(this.form.orderBalance) + Number(this.form.totalInterest)).toFixed(2);
        } else if (calcType === 'fullPrice' && this.form.fullPrice) {
            // 净价价格为空，全价价格有值,拿全价价格计算净价价格
            this.form.orderPrice = this.calOrderPrice(this.form.fullPrice, this.stockInfo).toFixed(2);
            // 净价金额
            this.form.orderBalance = (Number(this.form.orderPrice) * Number(this.form.orderAmount)).toFixed(2).toString();
            // 应计利息
            let interest = this.calcInterest(this.form.orderAmount, this.stockInfo.tDayInterest);
            // 总应计利息
            this.form.totalInterest = interest.toString();
            // 全价金额
            this.form.fullBalance = (Number(this.form.orderBalance) + Number(this.form.totalInterest)).toFixed(2);
        }
    }

    /**
     * 应计利息 = 指令数量 *T日百元利息
     */
    calcInterest(orderAmount, tDayInterest) {
        let result = Number(orderAmount) * Number(tDayInterest || 0);
        return result;
    }

    /**
     * 全价价格计算公式 全价价格=净价价格+T日百元利息(最新计算逻辑)
     */
    calcFullPrice(orderPrice, interest) {
        let result = String((Number(orderPrice || 0) + Number(interest || 0)).toFixed(2));
        if (result === 'NaN' || result === 'Infinity') {
            return '0';
        } else {
            return result;
        }
    }

    /**
     *  反推
     * 净价价格 = 全价价格- T日百元利息(新)
     */
    calOrderPrice(fullPrice, stockInfo) {
        return fullPrice - (stockInfo.tDayInterest || 0);
    }
}
</script>
<style lang="scss" scoped>
@import '@/pages/risk-measure/style/measure';
.font-color-red {
    /deep/ input {
        color: red !important;
    }
    /deep/ .el-input-group__append {
        color: red !important;
    }
}
.font-color-green {
    /deep/ input {
        color: green !important;
    }
    /deep/ .el-input-group__append {
        color: green !important;
    }
}
</style>
